package com.koron.css2.baseConfig.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ApplicationConfig;
import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.ConfigVacationBean;
import com.koron.css2.baseConfig.mapper.ConfigVacationMapper;
import com.koron.css2.baseConfig.queryBean.ConfigVacationQueryBean;
import com.koron.css2.baseConfig.vo.ConfigVacationVO;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.DbServerMapper;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.DBSourceUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.HashMap;
import java.util.List;

/**
 * 假期设置-列表初始化
 *
 * @author 廖大富
 */
public class ConfigVacationList implements ServerInterface {

    private static Logger logger = LogManager.getLogger(ConfigVacationList.class);

    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {

        @SuppressWarnings("rawtypes")
        MessageBean<PageInfo> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", PageInfo.class);
        try {
            ConfigVacationQueryBean bean = JsonUtils.objectToPojo(req.getData(), ConfigVacationQueryBean.class);
            ConfigVacationMapper mapper = factory.getMapper(ConfigVacationMapper.class);
            PageHelper.startPage(bean.getPage(), bean.getPageCount());
            List<ConfigVacationVO> list = mapper.selectConfigVacationList(bean);
            info.setData(new PageInfo<>(list));

        } catch (Exception e) {
            info.setCode(Constant.MESSAGE_INT_FAIL);
            info.setDescription(e.getMessage());
            logger.error(e.getMessage(), e);
        }
        return info;
    }

    //数据同步处理
    public static void synData(SessionFactory factory, ConfigVacationBean bean) {
        //获取公共库的信息
//        ConfigVacationMapper defaultMapper = factory.getMapper(ConfigVacationMapper.class, "_default");
//        List<ConfigVacationVO> dlist = defaultMapper.selectConfigVacationList(new ConfigVacationQueryBean());

        DbServerMapper dbMapper = factory.getMapper(DbServerMapper.class, "_default");
        List<String> companyList = dbMapper.selectCompany(ApplicationConfig.getWaterCodeFilter());
        for (String companyNo : companyList) {

            ConfigVacationMapper groupMapper = factory.getMapper(ConfigVacationMapper.class, DBSourceUtils.getDbEnv(companyNo));
            List<ConfigVacationVO> volist = groupMapper.selectConfigVacationList(new ConfigVacationQueryBean());
            HashMap<String, ConfigVacationVO> voHashMap = new HashMap<>();
            for (ConfigVacationVO vo : volist) {
                voHashMap.put(vo.getId(), vo);
            }
            if (voHashMap.containsKey(bean.getId())) {
//                ConfigVacationBean update = new ConfigVacationBean();
//                BeanUtils.copyProperties(update, bean);
                groupMapper.updateConfigVacation(bean);
            } else {
//                ConfigVacationBean insert = new ConfigVacationBean();
//                BeanUtils.copyProperties(insert, bean);
                groupMapper.insertConfigVacation(bean);
            }


        }
    }
}